package com.example.demo.permission.msg.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.common.UserUtil;
import com.example.demo.permission.msg.dao.SysMsgMapper;
import com.example.demo.permission.msg.pojo.MsgContent;
import com.example.demo.permission.msg.pojo.SysMsg;
import com.example.demo.permission.user.pojo.LtUser;
import com.example.demo.permission.user.service.LtUserService;

/**
 * 
 * <p>
 * 功能描述： 系统消息
 * </p>
 * 
 * @author lixh
 * @date 2019年3月13日 上午11:35:49
 * @version 1.0
 */
@Service
@Transactional
public class SysMsgService {

	@Autowired
	SysMsgMapper sysMsgMapper;

	@Autowired
	LtUserService ltUserService;

	@PreAuthorize("hasRole('ROLE_admin')")
	public boolean sendMsg(MsgContent content) {
		int result = sysMsgMapper.sendMsg(content);
		List<LtUser> list = ltUserService.getAllHr();
		int result2 = sysMsgMapper.addMsg2AllHr(list, content.getId().longValue());
		return result2 == list.size();
	}

	
	public List<SysMsg> getSysMsgByPage(Integer page, Integer size) {
		int start = (page - 1) * size;
		return sysMsgMapper.getSysMsg(start, size, UserUtil.getCurrentUser().getId().longValue());
	}

	public boolean markRead(Long flag) {
		if (flag != -1) {
			return sysMsgMapper.markRead(flag, UserUtil.getCurrentUser().getId().longValue()) == 1;
		}
		sysMsgMapper.markRead(flag, UserUtil.getCurrentUser().getId().longValue());
		return true;
	}

}
